package com.example.utils;

import com.alibaba.fastjson.JSONObject;
import com.example.entity.LoginUser;

/**
 * token工具类
 */
public class TokenUtil {

    // 本地线程缓存token
    private static ThreadLocal<String> local = new ThreadLocal<>();

    // 设置token
    public static void setToken(String content) {
        // 先移除
        removeToken();
        local.set(content);
    }

    // 获取token信息
    public static LoginUser getLoginUserByToken() {
        if (local != null) {
            return JSONObject.parseObject(local.get(), LoginUser.class);
        }
        return null;
    }

    // 移除token
    public static void removeToken() {
        if (local.get() != null)
            local.remove();
    }
}
